package com.emep.changzhi.analyse.service.basics;

import com.emep.changzhi.analyse.entity.basics.SysDepartmentInfo;
import com.emep.changzhi.analyse.entity.basics.SysLeadershipInfo;
import com.emep.changzhi.analyse.repository.basics.DepartmentRepository;
import com.emep.changzhi.analyse.repository.basics.LeadershipRepository;
import com.emep.changzhi.analyse.utils.Constant;
import com.emep.changzhi.analyse.utils.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: LFY
 * @version:V1.0
 * @Description:
 * @Date: 2018/8/28 15:15
 */

@Service
public class DepartmentService {

    @Autowired
    DepartmentRepository departmentRepository;

    public Page<SysDepartmentInfo> find(int pageIndex, int pageSize, Map<String, Object> params) {

        Page<SysDepartmentInfo> departmentPage = departmentRepository.findAll(new DepartmentSpec(params),
                PageRequest.of(pageIndex, pageSize, Sort.Direction.ASC, "epaId"));
        return departmentPage;
    }

    public boolean add(SysDepartmentInfo department) {
        List<SysDepartmentInfo> infos =departmentRepository.findAllByNameAndIsDelete(department.getName(), Constant.RESULT_DELETECODE_NO);
        if(infos.isEmpty()){
            departmentRepository.save(department);
            return true;
        }
        return false;
    }

    public SysDepartmentInfo getDepartment(long departmentId){
        return departmentRepository.findByDepartmentId(departmentId); }

    public boolean deleteDepartment(SysDepartmentInfo department){
        SysDepartmentInfo sysDepartmentInfo = departmentRepository.save(department);
        if(sysDepartmentInfo != null){
            return true;
        }else {
            return false;
        }
    }

    public boolean updateDepartment(SysDepartmentInfo department){
        List<SysDepartmentInfo> infos =departmentRepository.findAllByNameAndDepartmentIdNotAndEpaIdNotAndIsDelete(department.getName(),department.getDepartmentId(),department.getEpaId(),Constant.RESULT_DELETECODE_NO);
        if(infos.isEmpty()){
            departmentRepository.save(department);
            return true;
        }
        return false;
    }

    private class DepartmentSpec implements Specification<SysDepartmentInfo> {
        private Map<String, Object> params = new HashMap<String, Object>();

        public DepartmentSpec(Map<String, Object> params) {
            this.params = params;
        }

        @Override
        public Predicate toPredicate(Root<SysDepartmentInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            List<Predicate> lstPredicates = new ArrayList<Predicate>();

            if (params.containsKey("epaId")) {
                lstPredicates.add(cb.equal(root.get("epaId").as(String.class), params.get("epaId").toString()));
            }
            if (params.containsKey("leaderId")) {
                lstPredicates.add(cb.equal(root.get("leaderId").as(String.class), params.get("leaderId").toString()));
            }
            if (params.containsKey("name")) {
                lstPredicates.add(cb.like(root.get("name").as(String.class), params.get("name").toString()));
            }
            if (params.containsKey("isDelete")) {
                lstPredicates.add(cb.like(root.get("isDelete").as(String.class), params.get("isDelete").toString()));
            }

            Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
            return cb.and(lstPredicates.toArray(arrayPredicates));
        }
    }
}
